﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace ReportingManagement.Models
{
    /// <summary>
    /// Represents a single row about the indicator in the report.
    /// if the report has five indicators, then that report will have five instances
    /// of this class.
    /// </summary>
    public class IndicatorDefinition : ModelBase
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public string Definition
        {
            get
            {
                StringBuilder sBuilder = new StringBuilder();
                foreach (var i in components) sBuilder.Append(i.Name + " ");
                var d = sBuilder.ToString();
                return d.Trim();
            }
        }

        public string DefinitionSavingText
        {
            get
            {
                if (components.Count < 1) return null;
                //var firstComponent = components[0];
                //var lastComponent = components[components.Count - 1];
                //if (firstComponent.Tag == null) return null;
                //if (lastComponent.Tag == null) return null;

                try
                {
                    StringBuilder sBuilder = new StringBuilder();
                    foreach (var component in components)
                    {
                        string c = string.Empty;
                        if (component.Tag != null)
                        {
                            if (component.Name.StartsWith("[")) c = component.Name;
                            else
                            {
                                DataRowView r = (DataRowView)component.Tag;
                                int id = int.Parse(r["RepID"].ToString());
                                string shortCode = r["ShortCode"].ToString();
                                c = string.Format("{{{0}}}.{1}", id, shortCode);
                            }
                        }
                        else
                        {
                            c = string.Format(",{0},", component.Name);
                        }
                        sBuilder.Append(c);
                    }

                    string definition = sBuilder.ToString();
                    return definition;
                }
                catch { return null; }                
            }
        }

        private List<DefinitionComponent> components = new List<DefinitionComponent>();
        public void AddComponent(DefinitionComponent component)
        {
            components.Add(component);
            _CanAddIndicator = (component.Tag == null);
            _CanAddFixedNumber = (component.Tag == null);
            _CanAddOperator = (component.Tag != null);

            _CanRemoveIndicator = !_CanAddIndicator;
            _CanRemoveFixedNumber = !_CanAddFixedNumber;
            _CanRemoveOperator = !_CanAddOperator;

            OnPropertyChanged("Definition");
            OnPropertyChanged("CanAddOperator");
            OnPropertyChanged("CanRemoveOperator");
            OnPropertyChanged("CanAddIndicator");
            OnPropertyChanged("CanRemoveIndicator");
            OnPropertyChanged("CanAddFixedNumber");
            OnPropertyChanged("CanRemoveFixedNumber");
        }

        public void RemoveComponent()
        {
            if (components.Count > 0)
            {
                var component = components[components.Count - 1];
                components.Remove(component);

                _CanAddIndicator = (component.Tag != null);
                _CanAddFixedNumber = (component.Tag != null);
                _CanAddOperator = (component.Tag == null);

                _CanRemoveIndicator = !_CanAddIndicator;
                _CanRemoveFixedNumber = !_CanAddFixedNumber;
                _CanRemoveOperator = !_CanAddOperator;

                // This is the case when there is no component in the definition
                if (components.Count == 0)
                {
                    _CanAddIndicator = true;
                    _CanAddFixedNumber = true;
                    _CanAddOperator = false;
                    _CanRemoveIndicator = false;
                    _CanRemoveFixedNumber = false;
                    _CanRemoveOperator = false;
                }

                OnPropertyChanged("Definition");
                OnPropertyChanged("Definition");
                OnPropertyChanged("CanAddOperator");
                OnPropertyChanged("CanRemoveOperator");
                
                OnPropertyChanged("CanAddIndicator");
                OnPropertyChanged("CanRemoveIndicator");
                OnPropertyChanged("CanAddFixedNumber");
                OnPropertyChanged("CanRemoveFixedNumber");
            }
        }

        private bool _CanAddOperator = false;
        private bool _CanRemoveOperator = false;
        
        private bool _CanAddIndicator = true;
        private bool _CanRemoveIndicator = false;
        private bool _CanAddFixedNumber = true;
        private bool _CanRemoveFixedNumber = false;

        public bool CanAddOperator
        {
            get { return _CanAddOperator; }
        }

        public bool CanRemoveOperator
        {
            get { return _CanRemoveOperator; }
        }

        public bool CanAddIndicator
        {
            get { return _CanAddIndicator; }
        }

        public bool CanRemoveIndicator
        {
            get { return _CanRemoveIndicator; }
        }

        public bool CanAddFixedNumber
        {
            get { return _CanAddFixedNumber; }
        }

        public bool CanRemoveFixedNumber
        {
            get { return _CanRemoveFixedNumber; }
        }

        public override string ToString()
        {
            return Definition;
        }
    }
}
